﻿using System;
using System.Runtime.InteropServices;

namespace Letter.IO;

public static class BufferExtension
{
    public static ArraySegment<T> GetArray<T>(this Memory<T> memory)
    {
        return ((ReadOnlyMemory<T>)memory).GetArray();
    }

    public static ArraySegment<T> GetArray<T>(this ReadOnlyMemory<T> memory)
    {
        if (!MemoryMarshal.TryGetArray(memory, out var result))
        {
            throw new InvalidOperationException("Buffer backed by array was expected");
        }

        return result;
    }
}